package cn.innosmart.aq.view.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.RelativeLayout;

import com.yuntongxun.kitsdk.ECDeviceKit;

import cn.innosmart.aq.R;
import cn.innosmart.aq.adapter.FriendListAdapter;
import cn.innosmart.aq.bean.FriendBean;
import cn.innosmart.aq.customize.CustomsizeDialog;
import cn.innosmart.aq.view.activity.FriendActivity;

/**
 * Created by David on 2015/9/15.
 */
public class FriendFragment extends BaseFragment {
    private View view;
    private ExpandableListView lvFriends;
    private RelativeLayout rlAll;
    private FriendListAdapter friendListAdapter;
    private FriendActivity activity;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = (FriendActivity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_friend, container, false);
        lvFriends = (ExpandableListView) view.findViewById(R.id.lv_friends);
        //影藏下标箭头
        lvFriends.setGroupIndicator(null);
        //影藏竖直滚动条
        lvFriends.setVerticalScrollBarEnabled(false);
        /*隐藏选择的黄色高亮*/
        ColorDrawable drawable_tranparent_ = new ColorDrawable(Color.TRANSPARENT);
        lvFriends.setSelector(drawable_tranparent_);
        friendListAdapter = new FriendListAdapter(getActivity(), FriendActivity.groups, FriendActivity.childs);
        friendListAdapter.setOnItemClickListener(mItemOnClickListener);
        friendListAdapter.setOnLongClickListener(onLongClickListener);
        lvFriends.setAdapter(friendListAdapter);
        rlAll = (RelativeLayout) view.findViewById(R.id.rl_all);
        setHasOptionsMenu(true);
        setBar();
        return view;
    }

    private View.OnLongClickListener onLongClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            FriendBean friendBean = (FriendBean) v.getTag();
            removeDialog(friendBean);
            return true;
        }
    };
    private View.OnClickListener mItemOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FriendBean friendBean = (FriendBean) v.getTag();
            ECDeviceKit.getIMKitManager().startConversationActivity(friendBean.getId(), friendBean.getNickName());
        }
    };
    private Menu menus;
    /**
     * 设置Toolbar
     */
    private Toolbar toolbar;

    private void setBar() {
        toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
    }

    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            return true;
        }

    };

//    @Override
//    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//        getActivity().getMenuInflater().inflate(R.menu.menu_friend, menu);
//        menus = menu;
//        super.onCreateOptionsMenu(menu, inflater);
//    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onResume() {
//        showInfo();
        refresh();
        super.onResume();
    }

    //弹出提示层
    private void showInfo() {
        rlAll.setVisibility(View.VISIBLE);
    }

    //隐藏提示层
    private void hideInfo() {
        rlAll.setVisibility(View.GONE);
    }

    public void refresh() {
        if (friendListAdapter != null)
            friendListAdapter.refreshData();
    }

    private void removeDialog(final FriendBean friendBean) {
        String nickName = friendBean.getNickName();
        final CustomsizeDialog removeDialog = new CustomsizeDialog(getActivity());
        removeDialog.setTitle(getString(R.string.dialog_title_alert));
        removeDialog.setCancelable(false);
        removeDialog.setCanceledOnTouchOutside(false);
        removeDialog.setContent("确认删除" + nickName + "吗？");
        removeDialog.setPositiveButton(getString(R.string.sure), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.removeFriend(friendBean);
                removeDialog.dismiss();
            }
        });
        removeDialog.setNegativeButton(getString(R.string.cancel), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeDialog.dismiss();
            }
        });
        removeDialog.show();

    }
}
